package com.cyandpos.pad.cateringformat;


import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.changyi.device.APApplication;
import com.cyandpos.pad.R;
import com.cyandpos.pad.activity.BaseActivity;
import com.cyandpos.pad.activity.MyHandler;
import com.cyandpos.pad.cateringformat.choosemenu.MenuFoodFragment;
import com.cyandpos.pad.cateringformat.morefunction.MorefunctionFragment;
import com.cyandpos.pad.cateringformat.opentable.OpenTableFragment;
import com.cyandpos.pad.cateringformat.orderinfo.OrderInfoFragment;
import com.cyandpos.pad.common.commondata.Constant;
import com.cyandpos.pad.common.commondata.ConstantString;
import com.cyandpos.pad.common.commondata.DialogConstant;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.commondata.OnDialogListener;
import com.cyandpos.pad.common.commondata.SktConfigConstant;
import com.cyandpos.pad.common.commondata.SktConfigIdSaleTypeConstant;
import com.cyandpos.pad.common.commondata.SktConfigValueCateringConstant;
import com.cyandpos.pad.posdata.QueryMemberReq;
import com.cyandpos.pad.posdata.Sktconfig;
import com.cyandpos.pad.presentation.AdPresentationUtil;
import com.cyandpos.pad.sale.saleutils.SaleUtil;
import com.cyandpos.pad.sale.saleutils.TimeThread;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.log.SDTools;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CateringMainActivity extends BaseActivity {
    @BindView(R.id.iv_catering_main_project_logo)
    ImageView ivCateringMainProjectLogo;
    @BindView(R.id.tv_catering_main_storeName)
    TextView tvCateringMainStoreName;
    @BindView(R.id.tv_catering_main_title_version)
    TextView tvCateringMainTitleVersion;
    @BindView(R.id.tv_catering_main_title_cashier)
    TextView tvCateringMainTitleCashier;
    @BindView(R.id.tv_catering_main_title_sktno)
    TextView tvCateringMainTitleSktno;
    @BindView(R.id.tv_catering_main_title_orderno)
    TextView tvCateringMainTitleOrderno;
    @BindView(R.id.tv_catering_main_title_currentTime)
    public TextView tvCateringMainTitleCurrentTime;
    @BindView(R.id.iv_catering_main_wifi)
    ImageView ivCateringMainWifi;
    @BindView(R.id.iv_catering_main_printer)
    ImageView ivCateringMainPrinter;
    @BindView(R.id.iv_catering_main_exist)
    ImageView ivCateringMainExist;
    @BindView(R.id.btn_catering_main_openTable)
    LinearLayout btnCateringMainOpenTable;
    @BindView(R.id.btn_catering_main_chooseMenu)
    LinearLayout btnCateringMainChooseMenu;
    @BindView(R.id.btn_catering_main_orderInfo)
    LinearLayout btnCateringMainOrderInfo;
    @BindView(R.id.btn_catering_main_funtion)
    LinearLayout btnCateringMainFuntion;
    @BindView(R.id.container_catering_main)
    FrameLayout containerCateringMain;
    @BindView(R.id.viewpager_catering_main)
    ViewPager viewPagerCateringMain;

    private OpenTableFragment openTableFragment;
    private MenuFoodFragment choosemenuFragment;
    private MorefunctionFragment morefunctionFragment;
    private OrderInfoFragment orderInfoFragment;
    private int selectFunctionIndex=0;
    private int tableStatusIndex=0;

    final List<Fragment> fragmentList=new ArrayList<Fragment>();

    private final MyHandler handler = new MyHandler(this);
    public AdPresentationUtil adPresentationUtil;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_catering_main;
    }

    @Override
    protected void initData() {
        adPresentationUtil=new AdPresentationUtil();
        adPresentationUtil.initDisplayData(this);
        initTitleData();
        initFragment();
        initListenter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        settingDefaultFun(selectFunctionIndex);

    }

    private void initTitleData() {
        tvCateringMainTitleVersion.setText(CommonUtils.getCurrentVersionName(CateringMainActivity.this));
        tvCateringMainTitleCashier.setText(ErpCommonData.loadConfig.getCashierName() + "");
        tvCateringMainTitleSktno.setText(ErpCommonData.sktno + "");
        tvCateringMainTitleOrderno.setText(ErpCommonData.orderNo + "");
        Picasso.get().load(R.mipmap.shiji_log1).
                networkPolicy(NetworkPolicy.NO_CACHE, NetworkPolicy.NO_STORE)
                .placeholder(R.mipmap.shiji_log1).error(R.mipmap.shiji_log1)
                .into(ivCateringMainProjectLogo);
        TimeThread timeThread = new TimeThread(handler,Constant.CATERING_MAIN_UPDATE_CURRENT_TIME_HANDLEID);
        timeThread.setDaemon(true);//守护线程
        timeThread.start();
    }


    private void initListenter() {
        btnCateringMainOpenTable.setOnClickListener(new FunctionOnClickListener());
        btnCateringMainChooseMenu.setOnClickListener(new FunctionOnClickListener());
        btnCateringMainOrderInfo.setOnClickListener(new FunctionOnClickListener());
        btnCateringMainFuntion.setOnClickListener(new FunctionOnClickListener());
        ivCateringMainExist.setOnClickListener(new FunctionOnClickListener());

        viewPagerCateringMain.setCurrentItem(selectFunctionIndex);
        settingDefaultFun(selectFunctionIndex);
    }

    private void initFragment() {
        boolean isFastFood = false;//是否是快餐
        if(ErpCommonData.loadConfig.getSkt()!=null) {
            for (Sktconfig sktconfig : ErpCommonData.loadConfig.getSkt().getSktconfigList()) {
                if (sktconfig.getLx().equals(SktConfigConstant.SKT_CONFIG_TYPE_SALE)) {
                    if (sktconfig.getId().equals(SktConfigIdSaleTypeConstant.SKT_CONFIG_CATERING_POS)) {
                        if (sktconfig.getValue().equals(SktConfigValueCateringConstant.FASH_FOOD)) {
                            isFastFood = true;
                        }
                        break;
                    }
                }
            }
        }
        openTableFragment = new OpenTableFragment();
        choosemenuFragment = new MenuFoodFragment();
        orderInfoFragment = new OrderInfoFragment();
        morefunctionFragment = new MorefunctionFragment();

        if(isFastFood){
            btnCateringMainOpenTable.setVisibility(View.GONE);
            btnCateringMainChooseMenu.setVisibility(View.VISIBLE);
            fragmentList.add(choosemenuFragment);
        }else{
            btnCateringMainOpenTable.setVisibility(View.VISIBLE);
            btnCateringMainChooseMenu.setVisibility(View.GONE);
            fragmentList.add(openTableFragment);
        }
        fragmentList.add(orderInfoFragment);
        fragmentList.add(morefunctionFragment);
        viewPagerCateringMain.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }
            @Override
            public int getCount() {
                return fragmentList.size();
            }
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
            }
        });
        viewPagerCateringMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
//                滑动viewpager/Button随之滑动
                settingDefaultFun(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private class FunctionOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_catering_main_openTable:
                case R.id.btn_catering_main_chooseMenu:
                    selectFunctionIndex=0;
                    break;
                case R.id.btn_catering_main_orderInfo:
                    selectFunctionIndex=1;
                    break;
                case R.id.btn_catering_main_funtion:
                    selectFunctionIndex=2;
                    break;
                case R.id.iv_catering_main_exist://退出系统
                    SaleUtil.showInfoDialog(CateringMainActivity.this,
                            ConstantString.DIALOG_MAIN_SUSPEND_ORDER_FAIL_TITLE,
                            ConstantString.DIALOG_MAIN_EXIST_SYSTEM_MESSAGE,
                            new OnDialogListener() {
                                @Override
                                public void onClick(int flag) {
                                    if(flag == DialogConstant.DIALOG_BTN_SURE){
                                        if(ErpCommonData.ticketInfo.getTicketGoodCount()>0){
                                            SaleUtil.showMsgInto(CateringMainActivity.this,
                                                    ConstantString.DIALOG_MAIN_SUSPEND_ORDER_FAIL_TITLE,
                                                    ConstantString.DIALOG_MAIN_SWITCHING_PEOPLE_MESSAGE);
                                        }
                                        else if(SDTools.traverseFolderPick().size()>0){
                                            SaleUtil.showMsgInto(CateringMainActivity.this,
                                                    ConstantString.DIALOG_MAIN_SUSPEND_ORDER_FAIL_TITLE,
                                                    ConstantString.DIALOG_MAIN_SUSPEND_ORDER_SWITCHING_PEOPLE_MESSAGE);
                                        }
                                        else{
                                            APApplication.getInstance().exit();
                                        }
                                    }
                                }
                            });
                    break;
                default:
                    break;
            }
            viewPagerCateringMain.setCurrentItem(selectFunctionIndex);
            settingDefaultFun(selectFunctionIndex);

        }
    }

    public void settingDefaultFun(int count){
        if(btnCateringMainOpenTable.getVisibility() == View.VISIBLE){
            btnCateringMainOpenTable.setBackgroundColor(count==0? getResources().getColor(R.color.colorTableSelected):getResources().getColor(R.color.main_good_info_bar));
            openTableFragment.updateDinnerTableStatus();
        }else{
            btnCateringMainChooseMenu.setBackgroundColor(count==0? getResources().getColor(R.color.colorTableSelected):getResources().getColor(R.color.main_good_info_bar));
        }
        btnCateringMainOrderInfo.setBackgroundColor(count==1? getResources().getColor(R.color.colorTableSelected):getResources().getColor(R.color.main_good_info_bar));
        btnCateringMainFuntion.setBackgroundColor(count==2? getResources().getColor(R.color.colorTableSelected):getResources().getColor(R.color.main_good_info_bar));
    }

    public void queryMember(QueryMemberReq requestParam){
        choosemenuFragment.queryMember(requestParam);
    }

    public void setOrderNo(String orderNo){
        tvCateringMainTitleOrderno.setText(orderNo);
    }

}
